#include "CChara.h"
#include "CGame.h"

const float CChara::MOVE_SPEED=0.03f;
const int CChara::ANIMATION_DELAY=1000;
const int CChara::CHARA_WIDTH=64;
const int CChara::CHARA_HEIGHT=64;

CChara::CChara(){
	m_hp=0;
	m_animation=0;
	m_animationdelay=0;
	ZeroMemory(m_vec,sizeof(D3DXVECTOR3));
	ZeroMemory(m_vecbuf,sizeof(D3DXVECTOR3));
	m_vec.x=64.0f;
	m_vec.y=64.0f;
}
CPlayer::CPlayer(){
	key_flag=2;
	m_jpangle=90;
	m_jpflg=FALSE;
	m_jpdelay=0;
}

void CPlayer::KeyUpdate(){
	m_vecbuf=m_vec;
	nkey_flag[0]=GetAsyncKeyState(VK_LEFT)<0;
	nkey_flag[1]=GetAsyncKeyState(VK_RIGHT)<0;

	for(m_hoge=0;m_hoge<2;m_hoge++){
		if(lkey_flag[m_hoge]!=nkey_flag[m_hoge]&&!lkey_flag[m_hoge])
			break;
	}

	if(m_hoge==2&&key_flag!=2){
		for(m_hoge=0;m_hoge<2;m_hoge++){
			if(nkey_flag[(key_flag+m_hoge)%2]){
				m_hoge=(key_flag+m_hoge)%2;
				break;
			}
		}
	}

	switch(m_hoge){
	case 0:
		m_vec.x-=MOVE_SPEED;
		CollisionMap(0);
		if(!m_animationdelay || m_animation<=3){
			if(m_animation<=3){
				m_animation=4;
			}
			m_animation=4+(m_animation+1)%4;
			m_animationdelay=1000;
		}
		key_flag=0;
		break;
	case 1:
		m_vec.x+=MOVE_SPEED;
		CollisionMap(1);
		if(!m_animationdelay || m_animation>=4){
			if(m_animation>=4){
				m_animation=0;
			}
			m_animation=(m_animation+1)%4;
			m_animationdelay=1000;
		}
		key_flag=1;
		break;
	default:
		m_animation=m_animation/4*4;
		m_animationdelay=0;
		key_flag=2;
		break;
	}
	memcpy(lkey_flag,nkey_flag,sizeof(lkey_flag));
	if(m_animationdelay)
		m_animationdelay--;

	if(GetAsyncKeyState(VK_SPACE)<0 && !m_jpflg){
		m_jpdelay=0;
		m_jpangle=-1;
		m_jpflg=TRUE;
	}
	if(m_jpangle<180 && m_jpflg){
		m_jpangle+=0.03f;
		if(m_jpangle>180){
			m_jpangle=180;
		}
	}
	if(!m_jpdelay && m_jpflg){
		m_vec.y-=(float)sin((90+m_jpangle)*3.14159/180.0)+(float)sin((90+m_jpangle)*3.14159/180.0)+(float)sin((90+m_jpangle)*3.14159/180.0);
		m_jpdelay=50;
	}
	if(m_jpdelay)
		m_jpdelay--;
	if(m_jpangle<90){
		if(CollisionMap(3)){
			m_jpangle=90;
		}
	}else{
		if(CollisionMap(2)){
			m_jpangle=90;
			m_jpdelay=0;
			m_jpflg=FALSE;
		}else{
			m_jpflg=TRUE;
		}
	}
}

BOOL CChara::CollisionMap(int buf){
	for(int i=0;i<CMap::line_num;i++){
		for(int j=0;j<CMap::char_num;j++){
			if( m_vec.x<(float)(j+1)*CMap::MAP_WIDTH && m_vec.x+CHARA_WIDTH>(float)j*CMap::MAP_WIDTH &&
				m_vec.y<(float)(i+1)*CMap::MAP_HEIGHT && m_vec.y+CHARA_HEIGHT>(float)i*CMap::MAP_HEIGHT &&
				CGame::GetInstance().m_cmap.GetMap(j,i)==1){
					if(buf==0)
						m_vec.x=(float)(j+1)*CMap::MAP_WIDTH;
					else if(buf==1)
						m_vec.x=(float)j*CMap::MAP_WIDTH-CHARA_WIDTH;
					else if(buf==2)
						m_vec.y=(float)i*CMap::MAP_HEIGHT-CHARA_HEIGHT;
					else
						m_vec.y=(float)(i+1)*CMap::MAP_HEIGHT;
					return TRUE;
			}
		}
	}
	return FALSE;
}

void CChara::Draw(){
	m_sprite.SetPos(m_vec);
	m_sprite.Draw(CGame::GetInstance().m_PlayerTex->GetTexture(),(float)CHARA_HEIGHT,(float)CHARA_WIDTH,m_animation%4,m_animation/4);
}

void CChara::Update(){
}